#!usr/bin/env python  
# -*- coding:utf-8 _*-
""" 
@author:pengfeifu
@file: db_config.py 
@version:
@time: 2021/11/08 
@email:1324734112@qq.com
@desc： 读取数据库配置
@function：常用函数
"""
import os
import configparser


class Config(object):
    """
        # Config().get_content("user_information")
        配置文件里面的参数
        [db_mysql]
        host = 192.168.1.180
        port = 3306
        user = root
        password = 123456
    """

    def __init__(self, config_name="db_mysql_config.cnf"):
        # 获取配置文件全路径
        config_file_path = os.path.join(os.path.dirname(__file__), config_name)
        self.cf = configparser.ConfigParser()
        self.cf.read(config_file_path)

    # def get_sections(self):
    #     return self.cf.sections()

    # section ->配置文件中的[db_mysql]部分
    def get_option(self, section):
        config_list = self.cf.options(section)
        print(config_list, type(config_list))
        return config_list

    # 获取配置文件的内容，封装形成字典格式
    def get_content(self, section):
        config_result = {}
        for option in self.get_option(section):
            value = self.cf.get(section, option)
            config_result[option] = int(value) if value.isdigit() else value
        return config_result


if __name__ == "__main__":
    config = Config()
    option_dict = config.get_content("db_mysql")
    for option in option_dict:
        print("key:%s---------value:%s" % (option, option_dict.get(option)))
